package com.zggk.iroad.main.me;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.zggk.iroad.R;
import com.zggk.iroad.api.MyAppliCation;
import com.zggk.iroad.db.DaoManager;
import com.zggk.iroad.downdialog.DownBtnListener;
import com.zggk.iroad.downdialog.DownDialog;
import com.zggk.iroad.land.LandActivity;
import com.zggk.iroad.mvp.MVPBaseFragment;
import com.zggk.iroad.view.CircleImageView;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class MeFragment extends MVPBaseFragment<MeContract.View, MePresenter> implements MeContract.View {

    @Bind(R.id.my_icon)
    CircleImageView myIcon;
    @Bind(R.id.username_te)
    TextView usernameTe;
    @Bind(R.id.zzjg_te)
    TextView zzjgTe;
    @Bind(R.id.aqsz_te)
    TextView aqszTe;
    @Bind(R.id.aqsz_lay)
    LinearLayout aqszLay;
    @Bind(R.id.bbh_te)
    TextView bbhTe;
    @Bind(R.id.tcdl_te)
    TextView tcdlTe;
    private DownDialog downDialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fra_me, null);
        ButterKnife.bind(this, view);
        usernameTe.setText(MyAppliCation.spUtils.getString("OrgName"));
        zzjgTe.setText(MyAppliCation.spUtils.getString("GydwMc"));
        getVersionCode(getContext());
        return view;
    }

    private void getVersionCode(Context context) {
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packInfo;
        try {
            packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            bbhTe.setText("版本号：" + packInfo.versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRequestError(String msg) {
        showToast(msg, Toast.LENGTH_SHORT, Gravity.BOTTOM);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.tcdl_te)
    public void onViewClicked() {
        if (downDialog == null) {
            String title = "退出登录会清空当前数据";
            String okStr = "确定";
            String cancelStr = "取消";
            downDialog = new DownDialog(getActivity(), title, okStr, cancelStr, new DownBtnListener() {
                @Override
                public void CommOkBtnClick() {
                    dele();
                }

                @Override
                public void CommCancelBtnClick() {

                }
            });
        }
        downDialog.show();
    }
    private void dele() {
        DaoManager.getInstance().getSession().clear();
        MyAppliCation.spUtils.clear();
        startActivity(new Intent(getActivity(), LandActivity.class));
        getActivity().finish();
    }
}

